/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 3/19/2009
 * Time: 9:04 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.Collections;

namespace QuickExtractLink
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btn_browse;
		private System.Windows.Forms.TextBox txt_repTo;
		private System.Windows.Forms.TextBox txt_repFrom;
		private System.Windows.Forms.RadioButton rbtn_1;
		private System.Windows.Forms.RadioButton rbtn_2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox txt_from;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txt_file;
		private System.Windows.Forms.TextBox txt_text;
		private System.Windows.Forms.ListBox lbx_result;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txt_to;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.txt_to = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.lbx_result = new System.Windows.Forms.ListBox();
			this.txt_text = new System.Windows.Forms.TextBox();
			this.txt_file = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txt_from = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.rbtn_2 = new System.Windows.Forms.RadioButton();
			this.rbtn_1 = new System.Windows.Forms.RadioButton();
			this.txt_repFrom = new System.Windows.Forms.TextBox();
			this.txt_repTo = new System.Windows.Forms.TextBox();
			this.btn_browse = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// txt_to
			// 
			this.txt_to.Location = new System.Drawing.Point(88, 48);
			this.txt_to.Name = "txt_to";
			this.txt_to.Size = new System.Drawing.Size(200, 20);
			this.txt_to.TabIndex = 5;
			this.txt_to.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(184, 72);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(32, 23);
			this.label4.TabIndex = 2;
			this.label4.Text = "to";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(224, 360);
			this.button2.Name = "button2";
			this.button2.TabIndex = 11;
			this.button2.Tag = "Save";
			this.button2.Text = "Save";
			this.button2.Click += new System.EventHandler(this.ActionEvent);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(144, 360);
			this.button3.Name = "button3";
			this.button3.TabIndex = 10;
			this.button3.Tag = "Preview";
			this.button3.Text = "Preview";
			this.button3.Click += new System.EventHandler(this.ActionEvent);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.TabIndex = 2;
			this.label1.Text = "From token";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.TabIndex = 2;
			this.label2.Text = "To token";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 72);
			this.label3.Name = "label3";
			this.label3.TabIndex = 2;
			this.label3.Text = "Replace string";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lbx_result
			// 
			this.lbx_result.Location = new System.Drawing.Point(8, 24);
			this.lbx_result.Name = "lbx_result";
			this.lbx_result.Size = new System.Drawing.Size(288, 329);
			this.lbx_result.TabIndex = 8;
			this.lbx_result.Tag = "lbx";
			this.lbx_result.DoubleClick += new System.EventHandler(this.ActionEvent);
			this.lbx_result.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ListBox1KeyUp);
			// 
			// txt_text
			// 
			this.txt_text.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
						| System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txt_text.Location = new System.Drawing.Point(16, 87);
			this.txt_text.Multiline = true;
			this.txt_text.Name = "txt_text";
			this.txt_text.Size = new System.Drawing.Size(416, 457);
			this.txt_text.TabIndex = 0;
			this.txt_text.Text = "";
			// 
			// txt_file
			// 
			this.txt_file.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txt_file.Location = new System.Drawing.Point(80, 32);
			this.txt_file.Name = "txt_file";
			this.txt_file.ReadOnly = true;
			this.txt_file.Size = new System.Drawing.Size(272, 20);
			this.txt_file.TabIndex = 1;
			this.txt_file.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txt_file);
			this.groupBox1.Controls.Add(this.rbtn_1);
			this.groupBox1.Controls.Add(this.btn_browse);
			this.groupBox1.Controls.Add(this.txt_text);
			this.groupBox1.Controls.Add(this.rbtn_2);
			this.groupBox1.Location = new System.Drawing.Point(8, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(448, 560);
			this.groupBox1.TabIndex = 12;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Source";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.txt_repFrom);
			this.groupBox2.Controls.Add(this.txt_from);
			this.groupBox2.Controls.Add(this.label1);
			this.groupBox2.Controls.Add(this.txt_to);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Controls.Add(this.txt_repTo);
			this.groupBox2.Controls.Add(this.label4);
			this.groupBox2.Location = new System.Drawing.Point(464, 16);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(304, 160);
			this.groupBox2.TabIndex = 13;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Search conditions";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.button2);
			this.groupBox3.Controls.Add(this.lbx_result);
			this.groupBox3.Controls.Add(this.button3);
			this.groupBox3.Controls.Add(this.button1);
			this.groupBox3.Location = new System.Drawing.Point(464, 184);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(304, 392);
			this.groupBox3.TabIndex = 14;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Result";
			// 
			// txt_from
			// 
			this.txt_from.Location = new System.Drawing.Point(88, 24);
			this.txt_from.Name = "txt_from";
			this.txt_from.Size = new System.Drawing.Size(200, 20);
			this.txt_from.TabIndex = 4;
			this.txt_from.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 360);
			this.button1.Name = "button1";
			this.button1.TabIndex = 9;
			this.button1.Tag = "Extract";
			this.button1.Text = "Extract";
			this.button1.Click += new System.EventHandler(this.ActionEvent);
			// 
			// rbtn_2
			// 
			this.rbtn_2.Checked = true;
			this.rbtn_2.Location = new System.Drawing.Point(8, 64);
			this.rbtn_2.Name = "rbtn_2";
			this.rbtn_2.TabIndex = 3;
			this.rbtn_2.TabStop = true;
			this.rbtn_2.Tag = "srcText";
			this.rbtn_2.Text = "Text content";
			this.rbtn_2.CheckedChanged += new System.EventHandler(this.ActionEvent);
			// 
			// rbtn_1
			// 
			this.rbtn_1.Location = new System.Drawing.Point(8, 32);
			this.rbtn_1.Name = "rbtn_1";
			this.rbtn_1.TabIndex = 0;
			this.rbtn_1.Tag = "srcFile";
			this.rbtn_1.Text = "Text file";
			this.rbtn_1.CheckedChanged += new System.EventHandler(this.ActionEvent);
			// 
			// txt_repFrom
			// 
			this.txt_repFrom.Location = new System.Drawing.Point(88, 72);
			this.txt_repFrom.Name = "txt_repFrom";
			this.txt_repFrom.Size = new System.Drawing.Size(80, 20);
			this.txt_repFrom.TabIndex = 6;
			this.txt_repFrom.Text = "";
			// 
			// txt_repTo
			// 
			this.txt_repTo.Location = new System.Drawing.Point(208, 72);
			this.txt_repTo.Name = "txt_repTo";
			this.txt_repTo.Size = new System.Drawing.Size(80, 20);
			this.txt_repTo.TabIndex = 7;
			this.txt_repTo.Text = "";
			// 
			// btn_browse
			// 
			this.btn_browse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btn_browse.Enabled = false;
			this.btn_browse.Location = new System.Drawing.Point(360, 31);
			this.btn_browse.Name = "btn_browse";
			this.btn_browse.TabIndex = 2;
			this.btn_browse.Tag = "browse";
			this.btn_browse.Text = "Browse";
			this.btn_browse.Click += new System.EventHandler(this.ActionEvent);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(776, 597);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.Name = "MainForm";
			this.Text = "Quick Extract Link";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion
		void ActionEvent(object sender, System.EventArgs e)
		{
			string tag = ((Control)sender).Tag.ToString().ToLower();
			switch(tag){
				case "srctext": 					
					txt_text.ReadOnly = !((RadioButton)sender).Checked;
					break;
				case "srcfile":  
					txt_file.ReadOnly = !((RadioButton)sender).Checked;
					btn_browse.Enabled = ((RadioButton)sender).Checked;
					break;
				case "browse": 
					string file = FormLib.openFile("All File(*.*)|*.*");
					if( file != null ){
						txt_file.Text = file;
					}
					break;
				case "extract": doSearch();  break;
				case "lbx": 
					if( lbx_result.Items.Count > 0 && lbx_result.SelectedIndex >= 0 )
						System.Diagnostics.Process.Start( lbx_result.SelectedItem.ToString() );
					break;
				case "save": saveFile(); break;
				case "preview":  break;
			}
		}
		
		void ListBox1KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyValue == 46 && lbx_result.Items.Count > 0 && lbx_result.SelectedIndex >= 0 ){
				lbx_result.Items.RemoveAt( lbx_result.SelectedIndex );
			}
		}
		
		
		void doSearch(){
			string source = "";
			if( rbtn_1.Checked ){
				source = FileLib.readFile( txt_file.Text );
			}else{
				source = txt_text.Text;
			}
			
			ArrayList result = StringLib.getListGroupString( source, StringLib.convertSpecialChar(txt_from.Text), StringLib.convertSpecialChar(txt_to.Text) );						
			lbx_result.Items.Clear();
			
			if(txt_repFrom.Text == txt_repTo.Text){
				lbx_result.Items.AddRange( result.ToArray() );
			}else{				
				foreach( string item in result ){					
					lbx_result.Items.Add( item.Replace( StringLib.convertSpecialChar(txt_repFrom.Text), StringLib.convertSpecialChar(txt_repTo.Text) ) );
				}
			}
		}
		
		void saveFile(){
			string file = FormLib.saveFile("All File(*.html)|*.html");
			if( file != null ){
				string result = "";
				foreach( string item in lbx_result.Items ){
					result += string.Format( "<a href=\"{0}\">{0}</a><br>", item );
				}
				FileLib.writeFile( file, result );
				FormLib.Alert( "Save link to "+file+" completed." );
			}
		}
		
	}
}
